<template>
    <div class="loginPageSty">
        <div class="name">
            户享邦户型管理平台
        </div>
        <div class="formSty">
            <img src="../../assets/img/wechart_icon.png" class="iconSty" title="微信登录" @click="wechartLogin">
            <div style="height:60px;"></div>
            <div class="iptItem marginTop">
                <img src="@/assets/img/user.png" class="imgSty">
                <input placeholder="请输入用户名" v-model="form.userName" class="iptSty" />
            </div>
            <div class="iptItem">
                <img src="@/assets/img/password.png" class="imgSty">
                <input placeholder="请输入密码" type="password" v-model="form.password" class="iptSty" />
            </div>
            <div class="iptItem">
                <input placeholder="请输入验证码" type="verifyCode" v-model="form.verifyCode" class="iptSty addIptSty" />
                <el-button @click="getCode">{{ codeTxt }}</el-button>
            </div>
            <el-button class="btnSty" @click="goIndex">账号登录</el-button>
            <!-- <el-button class="btnSty" @click="wechartLogin">微信登录</el-button> -->
            <div class="forgetSty" @click="goRegister">点击注册</div>
        </div>
        <!-- <div class="bgSty flex_cen" v-if="codeUrl">
            <iframe :src="codeUrl" class="ifrSty" frameborder="0" scrolling="auto"></iframe>
        </div> -->
    </div>
    <el-dialog v-model="dialogVisible" title="绑定手机号" width="40%">
        <div class="flex_str">
            <div>手机号:</div>
            <el-input placeholder="请输入手机号" v-model="wechartPhone" style="width:80%;margin-left:10px;"></el-input>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false" style="margin-right:10px;">取消</el-button>
                <el-button type="primary" @click="bangdingBtn">
                    绑定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import { useRouter } from 'vue-router'
import { loginApi, getCodeApi, wechartLoginApi, loopCallbackApi, addLoginMobileApi } from '@/api/index/index';
import { ElMessage } from "element-plus";
const dialogVisible = ref(false)
const wechartPhone = ref('')

const router = useRouter()
const form = reactive({
    userName: '',
    password: '',
    verifyCode: '',
})
const codeTxt = ref<any>('获取验证码')
const codeUrl = ref('')
const randomSession = ref('')
const setInter = ref<any>(null)
const redirect = ref<any>(null)
const wechartLogin = () => {
    wechartLoginApi().then((res: any) => {
        codeUrl.value = res.data.redirect;
        randomSession.value = res.data.randomSession;
        setInter.value = setInterval(() => {
            loopCallbackFun()
        }, 1000)
        redirect.value = window.open(res.data.redirect)
    })
}
const bangdingBtn = () => {
    let myreg = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/;
    if (!myreg.test(wechartPhone.value)) {
        ElMessage.error('手机号输入错误')
        return
    }
    addLoginMobileApi({ mobile: wechartPhone.value }).then((res: any) => {
        if (res.status == 200) {
            dialogVisible.value = false
            router.push({ name: 'Index', path: '/' })
        }
    })
}
const loopCallbackFun = () => {
    loopCallbackApi({ randomSession: randomSession.value }).then((res: any) => {
        if (res.data.tag == null) {
            redirect.value.close()
            clearInterval(setInter.value)
            sessionStorage.setItem('token', res.data.tokenValue)
            dialogVisible.value = true
            return
        }
        if (res.data !== null) {
            redirect.value.close()
            clearInterval(setInter.value)
            sessionStorage.setItem('token', res.data.tokenValue)
            router.push({ name: 'Index', path: '/' })
        }
    })
}
const getCode = () => {
    const reg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/;
    if (!reg.test(form.userName)) {
        ElMessage.warning('手机号不正确')
        return
    }
    // codeTime()

    let params = {
        userName: form.userName
    }
    getCodeApi(params).then((res: any) => {
        codeTxt.value = res.data.verifyCode
    })
}
function goIndex() {
    loginApi(form).then((res: any) => {
        if (res.status == 200) {
            sessionStorage.setItem('token', res.data.tokenValue)
            router.push({ name: 'Index', path: '/' })
        }
    })
}
const goRegister = () => {
    router.push({ name: 'Register', path: '/register' })
}

</script>
  
<style scoped>
:deep(.el-button) {
    margin: 0;
}

.iconSty {
    position: absolute;
    right: 10px;
    top: 6px;
    width: 80px;
}

.addIptSty {
    width: 65%;
    margin-left: 12px;
}

.loginPageSty {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
    background: url("../../assets/img/login.png") 100% / cover no-repeat;
}

.name {
    font-size: 46px;
    color: #fff;
    margin-top: 94px;
    width: 578px;
    height: 110px;
    text-align: center;
}

.formSty {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-direction: column;
    /* width: 716px;
    height: 654px; */
    padding: 40px 80px;
    border: 1px solid #07D2E4;
    border-radius: 12px;
    position: relative;
    /* 背景图不对 透明部分不对称 */
    /* background: url("../../assets/img/form_bg.png") 100% / cover no-repeat; */
}


.iptItem {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    width: 374px;
    height: 64px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: rgba(0, 0, 0, 0.1);
    margin-bottom: 50px;
}


.imgSty {
    width: 32px;
    margin: 0 10px;
}

.iptSty {
    background-color: rgba(0, 0, 0, 0.1);
    border: none;
    color: #fff;
}

.iptSty:focus {
    outline: none;
}

.btnSty {
    width: 374px;
    height: 64px;
    background-color: #07D2E4;
    border: none;
    color: #fff;
    font-size: 28px;
    margin-top: 10px;
}

.forgetSty {
    font-size: 21px;
    color: #0ADFEF;
    margin-top: 20px;
}

.marginTop {
    margin-top: 6px;
}
</style>